package com.jmp.wx;

import com.alibaba.fastjson.JSONObject;
import com.jmp.api.dto.WxSubscribeDto;
import com.jmp.api.dto.WxSubscribeTypeEnum;
import lombok.extern.slf4j.Slf4j;
import one.stand.util.HttpUtil2;

import java.io.IOException;

@Slf4j
public class WxSubscribeUtil {
    private static final String WX_SUBCRIBE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";

    public static void send(WxSubscribeTypeEnum typeEnum, WxSubscribeDto subscribeDto) {
        // 装配post请求参数
        JSONObject json = new JSONObject();
        json.put("touser", subscribeDto.getTouser());
        json.put("template_id", typeEnum.getTemplateId());
        json.put("page", typeEnum.getPage(subscribeDto));
        json.put("data", JSONObject.parse(typeEnum.getData(subscribeDto)));
        try {
            String result = HttpUtil2.post(WX_SUBCRIBE + subscribeDto.getAccessToken(), json.toJSONString());
            log.info("wx subscribe result: {}", result);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}
